using System;

namespace ErrorDumper.Writing {
    public sealed class DirectoryExceptionWriter : IExceptionWriter {

        public string Directory { get; private set; }

        public DirectoryExceptionWriter() {
            Initialize();
        }

        private void Initialize() {
            Directory = ExceptionDataSource.Current.PhysicalPath;

            if (string.IsNullOrWhiteSpace(Directory))
                throw new InvalidOperationException("PhysicalPath property of ExceptionDataSource is not provided.");

            if (!System.IO.Directory.Exists(Directory))
                System.IO.Directory.CreateDirectory(Directory);

            if (!System.IO.Directory.Exists(Directory))
                throw new InvalidOperationException(string.Format("Could not create directory '{0}'", Directory));
        }

        public string WriteException(ExceptionPropertyCollection properties, string content) {
            return "file:" + ExceptionSerializer.Save(properties, content, Directory);
        }
    }
}
